<?php

/**
 * Created by PhpStorm.
 * User: osyun
 * Date: 2017/8/14
 * Time: 11:36
 */
namespace app\member_address\Model;

use think\Model;
use think\Db;

class MemberAddress extends Model
{
    /*
       * 删除收货地址
       * @param $data 删除条件
       */
    public function delAddress($where){

        //$result = Db::name('member_address')->where($where)->delete();
        $result = Db::name('member_address')->where($where)->update(['isdel'=>1]);
        return $result;
    }

    /*
     * 修改收货地址
     * @param $where 修改条件
     * @param $data 要修改成的数据
     * @return 返回影响数据的条数，没修改任何数据返回 0
     */
    public function updateAddress($where,$data){

        $result = Db::name('member_address')->where($where)->update($data);
        return $result;
    }

    /*
     * 设置为默认地址
     * @param $where 修改条件
     * @return 0失败 1成功
     */

    public function setDefaultAddress($where,$post){

        //开启事务
        Db::startTrans();
        try{

            $result1 = Db::name('member_address')->where(['openid'=>$where['openid']])->update(['isdefault'=>2]);
            //$result2 = Db::name('member_address')->where($where)->update(['isdefault'=>1]);
            $result2 = Db::name('member_address')->where($where)->update($post);
            if(!$result1 || !$result2){
                Db::rollback();
                return 0;
            }
            //提交事务
            Db::commit();
            return 1;
        }catch (\Exception $e) {

            // 回滚事务
            Db::rollback();
            return 0;
        }


    }

    /*
     * 获取默认地址
     * @return 查询结果不存在，返回 null
     */
    public function getDefaultAddress($where){
        $result = Db::name('member_address')->where($where)->field('id,openid,province,city,district,linkMan,address,mobile,code')->find();
        return $result;
    }

    /*
     *  验证传递的数据是否正确
     *  @param $data 接收到的参数
     *  @return 正确放回1,错误直接返回错误信息
     */
    public function validateDataAddress($data){
       // $commonmodel = new CommonModel();
        if(empty($data['openid'])){

            outFormats('','openid为空',0);

        }else if(empty($data['address'])){

            outFormats('','详细地址不能为空',0);
        }else if(empty($data['mobile'])){

            outFormats('','手机号不能为空',0);
        }else if(empty($data['linkMan'])){

            outFormats('','收货人姓名不能为空',0);
        }else if(empty($data['projectid'])){

            outFormats('','项目projectid不能为空',0);
        }else if(empty($data['province'])){

            outFormats('','省份不能为空',0);
        }else if(empty($data['city'])){

            outFormats('','城市不能为空',0);
        }else if(empty($data['district'])){

            outFormats('','区县不能为空',0);
        }else if($data['mobile']){

            if(!preg_match("/^1[34578]{1}\d{9}$/",$data['mobile'])){
                outFormats('',"手机号格式不正确",0);
            }
        }
        return 1;
    }

    /*
     * 获取多条数据
     * @param string $table  传去掉表前缀的表名
     * @param array  $where  查询条件
     * @param string $field  查询的字段
     * @param string $order  排序
     * @param int    $page_number 页数
     * @param int    $page_size  页面大小
     */

    public function getByWhereList($table,$where,$field,$order = 'id',$page_number=1,$page_size=10){
        $result = Db::name($table)
            ->field($field)
            ->where($where)
            ->order($order)
            ->page($page_number,$page_size)
            ->select();

        return $result;
    }

    public function getList($table,$where){

        $result = Db::name($table)->where($where)->paginate($page=10);
        return $result;
    }
}